<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      思路：背包容量为sum/2,如果sum%2!=0说明不能分割。
      求装满背包的最大价值是否等于背包容量
      
      */
      var canPartition = function (nums) {
        let sum = nums.reduce((previous, current) => {
          return (previous += current)
        }, 0)
        if (sum % 2 != 0) return false
        let bagSize = sum / 2
        let dp = new Array(bagSize + 1).fill(0)
        for (let i = 0; i < nums.length; i++) {
          for (let j = bagSize; j >= nums[i]; j--) {
            dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i])
          }
        }
        return dp[bagSize] == bagSize
      }
      canPartition([1, 5, 11, 5])
    </script>
  </body>
</html>
